﻿/*
 * 文件名称：
 * 程序员：wangyun
 * 创建时间：2008/07/23
 * 
 * 功能说明：页面跳转
 * 
 * 修改人员：
 * 
 * 修改日期：
 * 
 * 修改记录：
 */

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Whir.Common.WebHelper;

public enum MessageType : byte
{
    Information = 0,
    Warning = 1,
    Error = 2,
    FatalError = 3
}

public class PageHelper : System.Web.UI.Page
{
    public PageHelper()
    {
    }

    /// <summary>
    /// 页面调整
    /// </summary>
    /// <param name="page">Page</param>
    /// <param name="message">提示信息</param>
    public static void WriteMessage(Page page, string message)
    {
        WriteMessage(page, message, MessageType.Information, 0, null);
    }

    /// <summary>
    /// 页面调整
    /// </summary>
    /// <param name="page">Page</param>
    /// <param name="message">提示信息</param>
    /// <param name="type">信息类型</param>
    public static void WriteMessage(Page page, string message, MessageType type)
    {
        WriteMessage(page, message, type, 0, null);
    }

    /// <summary>
    /// 页面调整
    /// </summary>
    /// <param name="page">Page</param>
    /// <param name="message">提示信息</param>
    /// <param name="type">信息类型</param>
    /// <param name="targetUrl">跳转地址</param>
    public static void WriteMessage(Page page, string message, MessageType type, string targetUrl)
    {
        WriteMessage(page, message, type, 3, targetUrl);
    }

    /// <summary>
    /// 页面调整
    /// </summary>
    /// <param name="page">Page</param>
    /// <param name="message">提示信息</param>
    /// <param name="type">信息类型</param>
    /// <param name="waitSeconds">等待时间</param>
    /// <param name="targetUrl">跳转地址</param>
    public static void WriteMessage(Page page, string message, MessageType type, int waitSeconds, string targetUrl)
    {
        if (waitSeconds > 0)
            PageHelper.RenderRedirectingMessage(page, page.Server.HtmlEncode(message), type, waitSeconds, targetUrl);
        else
            PageHelper.RenderStaticMessage(page, page.Server.HtmlEncode(message), type);

        HttpContext.Current.ApplicationInstance.CompleteRequest();
    }

    /// <summary>
    /// 执行本页面弹出信息
    /// </summary>
    /// <param name="page">Page</param>
    /// <param name="message">提示信息</param>
    /// <param name="type">信息类型</param>
    internal static void RenderStaticMessage(Page page, string message, MessageType type)
    {
        if (page.Form == null)
            return;

        HtmlContainerControl control = FindControlEx(page.Form, "divMessage") as HtmlContainerControl;
        if (control == null && !page.ClientScript.IsStartupScriptRegistered("msg"))
        {
            page.ClientScript.RegisterStartupScript(page.GetType(), "msg", string.Format(
@"window.alert('{0}');", JavascriptEncode(message)), true);
        }
        else
        {
            RenderMessage(control, message, type);
        }
    }

    /// <summary>
    /// 执行跳转提示信息
    /// </summary>
    /// <param name="page">Page</param>
    /// <param name="message">提示信息</param>
    /// <param name="type">信息类型</param>
    /// <param name="waitSeconds">等待时间</param>
    /// <param name="url">跳转地址</param>
    internal static void RenderRedirectingMessage(Page page, string message, MessageType type, int waitSeconds, string url)
    {
        HtmlForm form = page.Form;
        if (page.Form == null)
            return;

        form.Controls.Clear();
        string target = page.ResolveClientUrl(url);
        HtmlContainerControl control = new HtmlGenericControl("div");
        RenderMessage(control, string.Format("{0}。系统将在 {1} 秒后跳转到新页面。<br /><a href=\"{2}\">点这里立即跳转</a>。", message, waitSeconds, target), type);

        form.Controls.Add(control);
        HtmlMeta meta = new HtmlMeta();
        meta.HttpEquiv = "refresh";
        meta.Content = string.Format("{0}; url={1}", waitSeconds, target);
        page.Header.Controls.Add(meta);
    }

    /// <summary>
    /// 获取指定控件
    /// </summary>
    /// <param name="c"></param>
    /// <param name="id"></param>
    /// <returns></returns>
    public static Control FindControlEx(Control c, string id)
    {
        Control control = c.FindControl(id);
        if (control != null)
        {
            return control;
        }
        foreach (Control ctrl in c.Controls)
        {
            control = FindControlEx(ctrl, id);
            if (control != null)
            {
                return control;
            }
        }
        return null;
    }

    internal static void RenderMessage(HtmlContainerControl control, string message, MessageType type)
    {
        control.Attributes["class"] = string.Format("message {0}", type.ToString());
        control.InnerHtml = "<div>" + message + "</div>";
        control.Visible = true;
    }

    public static string JavascriptEncode(string str)
    {
        if (str == null)
        {
            return string.Empty;
        }
        return str.Replace("'", @"\'").Replace("\"", "\\\"").Replace("\r\n", @"\n").Replace("\n", @"\n");
    }
}
